package com.sun.chart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.sun.base.UserActivity;
import com.sun.net.LinkServer;
import com.sun.net.BaseDao.HttpListener;

public class ChartImageActivity extends UserActivity implements OnItemClickListener{
	private SimpleAdapter adapter;
	private ListView listView;
	ArrayList<HashMap<String, String>>dataArrayList;
	@Override
	public void init() {noTitle = true;
		final RelativeLayout linearLayout = new RelativeLayout(this);
		ProgressBar progressBar = new ProgressBar(this);
		RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams(-2,-2);
		lParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		linearLayout.addView(progressBar,lParams);
		mainLayout.addView(linearLayout,new LinearLayout.LayoutParams(-1,-1));
		dataArrayList = new ArrayList<HashMap<String,String>>();
		listView = new ListView(this);
		listView.setCacheColorHint(0);
		LinkServer lServer = new LinkServer(handler);
		lServer.SearchZSListInfos(lotterId, 0, new HttpListener() {			
			@Override
			public void onEvent(List<String> list) {
				if (LinkServer.servertrue(list)) {
					for (int i = 0; i < (list.size()-2)/3; i++) {
						HashMap<String, String>map = new HashMap<String, String>();
						map.put("id", list.get(i*3+2));
						map.put("name", list.get(i*3+2+1));
						map.put("address", list.get(i*3+2+2));
						dataArrayList.add(map);
					}adapter = new SimpleAdapter(ChartImageActivity.this, dataArrayList, android.R.layout.simple_list_item_1, new String[]{"name"}, new int[]{android.R.id.text1});
					listView.setAdapter(adapter);
								
					mainLayout.addView(listView,new LinearLayout.LayoutParams(-1,-1));	
				}else 
				{
					TextView textView = new TextView(ChartImageActivity.this);
					textView.setText("获取走势图列表失败");
					mainLayout.addView(textView,new LinearLayout.LayoutParams(-1,-1));	
					
				}
				mainLayout.removeView(linearLayout);				
				
			}
		});
		
		listView.setOnItemClickListener(this);
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		HashMap<String, String>map = (HashMap<String, String>)adapter.getItem(arg2);
		Intent intent = new Intent();
		intent.putExtra("address", map.get("address"));
		intent.putExtra("id", map.get("id"));	
		intent.putExtra("name", map.get("name"));
		intent.setClass(this, ShowChartImageActivity.class);
		startActivity(intent);
	}

}
